using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Platform.ChooseTextPanel;

namespace Platform.algorithm
{
    abstract class KnowledgeBase
    {
        protected ArrayList ArrList;
        protected Dictionary<string, int> BaseFrequency;
        protected Dictionary<string, int> BaseBihua;
        private static KnowledgeBase kbase;
        public static KnowledgeBase Create()
        {
            string type = "File";
            if (KnowledgeBase.kbase == null)
            {
                if (type == "File")
                    KnowledgeBase.kbase = new KnowledgeBase_File();
                else if (type == "DB")
                    KnowledgeBase.kbase = new KnowledgeBase_DB();
                else throw new Exception("Cannot find type");
            }
            return KnowledgeBase.kbase;
        }
        static public void Reload()
        {
            KnowledgeBase.Create().Dispose();
            KnowledgeBase.kbase = null;
            KnowledgeBase.Create();
            //HashTabel.Dispose();
        }
        protected void Dispose()
        {
            ArrList.Clear();
            this.BaseFrequency.Clear();
            this.BaseBihua.Clear();
        }
        protected KnowledgeBase()
        {
            ArrList = new ArrayList();
            this.BaseBihua = new Dictionary<string, int>();
            this.BaseFrequency = new Dictionary<string, int>();
        }
        public int GetUnitFrequency(string ucode)
        {
            if (ucode.Length > 2) ucode = ucode.Substring(0, 2);
            if(this.BaseFrequency.ContainsKey(ucode))
                return this.BaseFrequency[ucode];
            return 0;
        }
        public int GetUnitBihua(string ucode)
        {
            if (ucode.Length > 2) ucode = ucode.Substring(0, 2);
            if (this.BaseBihua.ContainsKey(ucode))
                return this.BaseBihua[ucode];
            return 0;
        }
        public Element GetKonwledgeItemFromPlatformCode(string code)
        {
            if (ArrList.Count == 0) return null;
            int start = 0;
            int end = ArrList.Count - 1;
            while (start <= end)
            {
                int middle = (start + end) / 2;
                Element e = (Element)ArrList[middle];
                int cmp = code.CompareTo(e.SecondCode);
                if (cmp < 0) end = middle - 1;
                else if (cmp > 0) start = middle + 1;
                else return e.Colne();
            }
            return null;
        }
        public Element GetKonwledgeItemFromUnicode(string code)
        {
            for (int i = 0; i < ArrList.Count; ++i)
            {
                Element e = (Element)ArrList[i];
                if (e.Unicode == code)
                    return e.Colne();
            }
            return null;
        }
        public ArrayList SearchCode(string code)
        {
            ArrayList arr = new ArrayList();
            for (int i = 0; i < ArrList.Count; ++i)
            {
                Element e = (Element)ArrList[i];
                if (e.FirstCode.IndexOf(code) == 0)
                {
                    arr.Add(e);
                }
            }
            return arr;
        }
        public bool CodeComplete(string code)
        {
            int counter = 0;
            for (int i = 0; i < ArrList.Count; ++i)
            {
                Element e = (Element)ArrList[i];
                if (e.FirstCode.IndexOf(code) == 0)
                {
                    ++counter;
                    if (counter > 1) return false;
                }
            }
            return true;
        }
        abstract protected void getAllKnowledge();
    }
}
